package com.dynamixsoftware.printershare;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Picture;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.Button;
import com.dynamixsoftware.printershare.ActivityPrintWeb;
import com.dynamixsoftware.printershare.ActivityRoot;
import com.dynamixsoftware.printershare.App;
import com.dynamixsoftware.printershare.gmail.GmsProtos;
import com.dynamixsoftware.printershare.gmail.ProtoBuf;
import java.io.File;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ActivityGmailConversation extends ActivityRoot {

	public static final SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy h:mm a", new DateFormatSymbols(
			Locale.US));
	public static volatile Picture pp;
	private long client_id;
	private CookieManager cm;
	private long conversation_id;
	private String[] credentials;
	private String labels;
	private long s_label_id;
	private File tf = new File(App.getTempDir(), "printershare_gmatt.tmp");
	private long u_label_id;
	private WebView vw;
	private Thread wt;

	private void appendMessage(ProtoBuf var1, StringBuilder var2, int var3, boolean var4, boolean var5) {
		StringBuilder var6 = (new StringBuilder()).append("<table id=\"mp_").append(var3).append("\" border=\"0\" ");
		String var7;
		if (var4) {
			var7 = "style=\"display:none\" ";
		} else {
			var7 = "";
		}

		var2.append(var6.append(var7)
				.append("cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr><td class=\"headerDiv\">").toString());
		var2.append("<table border=\"0\" cellpadding=\"0\" width=\"100%\"><tr>");
		StringBuilder var10 = (new StringBuilder()).append("<td><img src=\"file:///android_asset/star_");
		String var11;
		if (var5) {
			var11 = "on";
		} else {
			var11 = "off";
		}

		var2.append(var10.append(var11).append(".png\"></td>").toString());
		var2.append("<td width=\"100%\" class=\"fromDiv\">&nbsp;");
		ProtoBuf var14 = var1.getProtoBuf(2);
		String var15 = TextUtils.htmlEncode(var14.getString(2) + " <" + var14.getString(1) + ">");
		var2.append("<a onclick=\"toggleHeader(\'" + var1.getLong(1) + "\')\">");
		var2.append(var15);
		var2.append("</a></td>");
		int var19 = var1.getCount(15);
		if (var19 > 0) {
			var2.append("<td><img src=\"file:///android_asset/paperclip.png\"></td>");
		}

		var2.append("<td nowrap=\"nowrap\">");
		Date var21 = new Date(var1.getLong(3));
		var2.append(df.format(var21));
		var2.append("</td>");
		var2.append("</tr></table>");
		StringBuilder var25 = (new StringBuilder()).append("<table id=\"").append(var1.getLong(1)).append("_hl2\" ");
		String var26;
		if (var4) {
			var26 = "style=\"display:none\" ";
		} else {
			var26 = "";
		}

		var2.append(var25.append(var26).append("border=\"0\" cellpadding=\"0\" width=\"100%\">").toString());
		int var28 = var1.getCount(9);

		for (int var29 = 0; var29 < var28; ++var29) {
			var2.append("<tr>");
			StringBuilder var86 = (new StringBuilder()).append("<td width=\"30px\" align=\"right\">");
			String var87;
			if (var29 == 0) {
				var87 = "To:";
			} else {
				var87 = " ";
			}

			var2.append(var86.append(var87).append("</td>").toString());
			var2.append("<td>&nbsp;");
			ProtoBuf var90 = var1.getProtoBuf(9, var29);
			var2.append(TextUtils.htmlEncode(var90.getString(2) + " <" + var90.getString(1) + ">"));
			var2.append("</td>");
			var2.append("</tr>");
		}

		int var30 = var1.getCount(10);

		for (int var31 = 0; var31 < var30; ++var31) {
			var2.append("<tr>");
			StringBuilder var77 = (new StringBuilder()).append("<td width=\"30px\" align=\"right\">");
			String var78;
			if (var31 == 0) {
				var78 = "Cc:";
			} else {
				var78 = " ";
			}

			var2.append(var77.append(var78).append("</td>").toString());
			var2.append("<td>&nbsp;");
			ProtoBuf var81 = var1.getProtoBuf(10, var31);
			var2.append(TextUtils.htmlEncode(var81.getString(2) + " <" + var81.getString(1) + ">"));
			var2.append("</td>");
			var2.append("</tr>");
		}

		int var32 = var1.getCount(11);

		for (int var33 = 0; var33 < var32; ++var33) {
			var2.append("<tr>");
			StringBuilder var68 = (new StringBuilder()).append("<td width=\"30px\" align=\"right\">");
			String var69;
			if (var33 == 0) {
				var69 = "Bcc:";
			} else {
				var69 = " ";
			}

			var2.append(var68.append(var69).append("</td>").toString());
			var2.append("<td>&nbsp;");
			ProtoBuf var72 = var1.getProtoBuf(11, var33);
			var2.append(TextUtils.htmlEncode(var72.getString(2) + " <" + var72.getString(1) + ">"));
			var2.append("</td>");
			var2.append("</tr>");
		}

		var2.append("</table>");
		StringBuilder var35 = (new StringBuilder()).append("<div id=\"").append(var1.getLong(1))
				.append("_snp\" class=\"snpDiv\"");
		String var36;
		if (var4) {
			var36 = "";
		} else {
			var36 = " style=\"display:none\"";
		}

		var2.append(var35.append(var36).append(">").toString());
		var2.append(TextUtils.htmlEncode(var1.getString(6)));
		var2.append("</div>");
		var2.append("</td><tr><td class=\"bodyCell\">");
		StringBuilder var41 = (new StringBuilder()).append("<div id=\"").append(var1.getLong(1)).append("_msg\" ");
		String var42;
		if (var4) {
			var42 = "style=\"display:none\" ";
		} else {
			var42 = "";
		}

		var2.append(var41.append(var42).append("class=\"bodyDiv\">").toString());
		var2.append(var1.getString(13));
		if (var19 > 0) {
			var2.append("<hr>");

			for (int var48 = 0; var48 < var19; ++var48) {
				ProtoBuf var49 = var1.getProtoBuf(15, var48);
				String var50 = var49.getString(3);
				int var51 = var50.indexOf(";");
				if (var51 > 0) {
					var50 = var50.substring(0, var51);
				}

				Uri var52 = Uri.parse("file://" + this.tf.getAbsolutePath() + "?msg_id=" + var1.getLong(1) + "&att_id="
						+ var49.getString(1) + "&content_type=" + var50 + "&size=" + var49.getInt(5));
				Intent var53 = new Intent("android.intent.action.VIEW");
				var53.setDataAndType(var52, var50);
				List var55 = this.getPackageManager().queryIntentActivities(var53, 65536);
				int var56 = 0;

				boolean var58;
				while (true) {
					int var57 = var55.size();
					var58 = false;
					if (var56 >= var57) {
						break;
					}

					ResolveInfo var66 = (ResolveInfo) var55.get(var56);
					if (var66.activityInfo != null
							&& var66.activityInfo.packageName.startsWith("com.dynamixsoftware.printershare")) {
						var58 = true;
						break;
					}

					++var56;
				}

				var2.append("<div class=\"attachmentDiv\">");
				var2.append("<img valign=\"middle\" src=\"file:///android_asset/attachment.png\">");
				var2.append("&nbsp;&nbsp;&nbsp;");
				if (var58) {
					var2.append("<a onclick=\"location=\'" + var52 + "\'\">");
				}

				var2.append("<b>" + var49.getString(2) + "</b>");
				if (var58) {
					var2.append("</a>");
				}

				var2.append("</div>");
			}
		}

		var2.append("</div>");
		var2.append("</td></tr></table>");
	}

	private String buildConversation(ProtoBuf var1) {
		StringBuilder var2 = new StringBuilder();
		var2.append("<html><head><meta name=\"viewport\" content=\"width=650, target-densitydpi=medium-dpi\"><link type=\"text/css\" rel=\"stylesheet\" href=\"file:///android_asset/styles.css\"><script type=\"text/javascript\" src=\"file:///android_asset/script.js\"></script></head><body>");
		int var4 = 0;
		int var5 = -1 + var1.getCount(11);

		for (int var6 = 0; var4 <= var5; ++var4) {
			ProtoBuf var8 = var1.getProtoBuf(11, var4);
			if (var4 == 0) {
				var2.append("<div class=\"conversationHeaderDiv\" style=\"width:auto\">");
				var2.append(TextUtils.htmlEncode(var8.getString(5)));
				var2.append("<br><div id=\"labels\">");
				String[] var37 = this.labels.split("\\ ");

				for (int var38 = 0; var38 < var37.length; ++var38) {
					var2.append("<span class=\"labelsDiv\">");
					var2.append(TextUtils.htmlEncode(var37[var38]));
					var2.append("</span>&nbsp;");
				}

				var2.append("</div></div>");
			}

			boolean var9 = false;
			boolean var10 = false;
			int var11 = var8.getCount(14);

			for (int var12 = 0; var12 < var11; ++var12) {
				long var32 = var8.getLong(14, var12);
				if (this.s_label_id == var32) {
					var10 = true;
				}

				if (this.u_label_id == var32) {
					var9 = true;
				}
			}

			if (!var9 && var4 != var5) {
				this.appendMessage(var8, var2, var4, true, var10);
				++var6;
			} else {
				if (var6 > 0) {
					int var13 = var4 - var6;
					String var14 = "_collapsed" + var13;
					var2.append("<div id=\"");
					var2.append(var14);
					var2.append("\"><a onClick=\"uncollapse(\'");
					var2.append(var14);
					var2.append("\', ");
					var2.append(var13);
					var2.append(", ");
					var2.append(var4 - 1);
					var2.append(")\"><div class=\"superCollapsedDiv size");
					String var24;
					if (var6 == 1) {
						var24 = "1";
					} else if (var6 == 2) {
						var24 = "2";
					} else {
						var24 = "n";
					}

					var2.append(var24);
					var2.append("\"><div class=\"superCollapsedLabel\">");
					var2.append(var6);
					StringBuilder var28 = (new StringBuilder()).append(" read message");
					String var29;
					if (var6 > 1) {
						var29 = "s";
					} else {
						var29 = "";
					}

					var2.append(var28.append(var29).toString());
					var2.append("</div></div></a></div>");
				}

				this.appendMessage(var8, var2, var4, false, var10);
				var6 = 0;
			}
		}

		var2.append("</body></html>");
		return var2.toString();
	}

	public void onCreate(Bundle var1) {
		super.onCreate(var1);
		this.setContentView(2130903054);
		this.setTitle(2131099668);
		this.vw = (WebView) this.findViewById(2131230769);

		try {
			CookieSyncManager.createInstance(this);
			this.cm = CookieManager.getInstance();
			if (this.cm != null) {
				this.cm.setAcceptCookie(true);
			}
		} catch (Exception var9) {
			var9.printStackTrace();
			App.reportThrowable(var9);
		}

		this.vw.setWebViewClient(new WebViewClient() {
			
			public boolean shouldOverrideUrlLoading(WebView var1, String var2) {
				return skip_update;
				// $FF: Couldn't be decompiled
			}
		});
		this.vw.setHorizontalScrollBarEnabled(true);
		this.vw.getSettings().setJavaScriptEnabled(true);
		this.vw.getSettings().setBlockNetworkImage(false);
		this.vw.getSettings().setLoadsImagesAutomatically(true);
		this.vw.getSettings().setSupportMultipleWindows(false);
		this.vw.getSettings().setBuiltInZoomControls(false);
		this.vw.getSettings().setSupportZoom(false);
		this.vw.getSettings().setUseWideViewPort(true);
		this.vw.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
		this.vw.loadDataWithBaseURL((String) null, "<html><body><b>" + this.getResources().getString(2131099757)
				+ "</b></body></html>", "text/html", "UTF-8", (String) null);
		((Button) this.findViewById(2131230726)).setOnClickListener(new OnClickListener() {
			public void onClick(View var1) {
				Picture var2 = ActivityGmailConversation.this.vw.capturePicture();
				if (var2 != null) {
					try {
						ActivityGmailConversation.pp = var2;
						Intent var4 = new Intent("gmail");
						var4.setClass(ActivityGmailConversation.this, ActivityPrintWeb.class);
						ActivityGmailConversation.this.startActivityForResult(var4, 10);
					} catch (Exception var5) {
						ActivityGmailConversation.pp = null;
						var5.printStackTrace();
						App.reportThrowable(var5);
						return;
					}
				}

			}
		});
		Intent var3 = this.getIntent();
		String var4 = var3.getExtras().getString("credentials");
		String[] var5 = null;
		if (var4 != null) {
			var5 = var4.split(" ");
		}

		this.credentials = var5;
		this.client_id = var3.getExtras().getLong("client_id");
		this.conversation_id = Long.parseLong(var3.getExtras().getString("conversation_id"));
		this.labels = var3.getExtras().getString("labels");
		if (this.credentials != null && this.credentials.length == 2) {
			this.s_label_id = var3.getExtras().getLong("s_label_id");
			this.u_label_id = var3.getExtras().getLong("u_label_id");
			final String[] var6 = GmsProtos.createUrlAndCookie(this.credentials);
			if (this.cm != null) {
				try {
					this.cm.setCookie("mail.google.com", var6[1]);
				} catch (Exception var8) {
					var8.printStackTrace();
					App.reportThrowable(var8);
				}
			}

			this.wt = new Thread() {
				public void run() {
					try {
						Thread.sleep(500L);
					} catch (InterruptedException var11) {
						;
					}

					try {
						ProtoBuf var2 = new ProtoBuf(GmsProtos.REQUEST);
						var2.setLong(1, ActivityGmailConversation.this.client_id);
						ProtoBuf var5 = var2.setNewProtoBuf(3);
						ProtoBuf var6x = var5.setNewProtoBuf(1);
						var6x.setLong(1, ActivityGmailConversation.this.conversation_id);
						var6x.setLong(2, 0L);
						var5.setLong(4, ActivityGmailConversation.this.conversation_id);
						ProtoBuf var7 = var2.setNewProtoBuf(4);
						var7.setLong(1, 0L);
						var7.setLong(2, 0L);
						var7.setLong(3, 200L);
						var7.setBool(6, true);
						var7.setInt(7, 0);
						var7.setBool(8, true);
						var7.setBool(9, true);
						var2.setNewProtoBuf(7).setLong(2, 0L);
						ProtoBuf var8 = GmsProtos.doRequest(ActivityGmailConversation.this.credentials, var2);
						final String var9 = ActivityGmailConversation.this.buildConversation(var8);
						ActivityGmailConversation.this.runOnUiThread(new Runnable() {
							public void run() {
								if (!ActivityGmailConversation.this.isFinishing()) {
									ActivityGmailConversation.this.vw.loadDataWithBaseURL(var6[0], var9, "text/html",
											"UTF-8", (String) null);
								}

							}
						});
					} catch (Exception var10) {
						var10.printStackTrace();
						App.reportThrowable(var10);
						ActivityGmailConversation.this.setResult(0);
						ActivityGmailConversation.this.finish();
					}

					ActivityGmailConversation.this.wt = null;
				}
			};
			this.wt.start();
		} else {
			this.setResult(0);
			this.finish();
		}
	}
}
